package leetcode_bin_tree_test;

import bin_tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 872. 叶子相似的树
 */
public class LeafSimilar {
    public static boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> list1 = new LinkedList<>();
        find(root1, list1);
        List<Integer> list2 = new LinkedList<>();
        find(root2, list2);
        if (list1.size() != list2.size()) {
            return false;
        }
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i) != list2.get(i)) {
                return false;
            }
        }
        return true;
    }

    public static void find(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        find(root.left, list);
        if (root.left == null && root.right == null) {
            list.add(root.val);
        }
        find(root.right, list);
    }

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(3);
        TreeNode n1 = new TreeNode(5);
        TreeNode n2 = new TreeNode(1);
        TreeNode n3 = new TreeNode(6);
        TreeNode n4 = new TreeNode(2);
        TreeNode n5 = new TreeNode(9);
        TreeNode n6 = new TreeNode(8);
        TreeNode n7 = new TreeNode(7);
        TreeNode n8 = new TreeNode(4);
        root1.left = n1;
        root1.right = n2;
        n1.left = n3;
        n1.right = n4;
        n2.left = n5;
        n2.right = n6;
        n4.left = n7;
        n4.right = n8;
        TreeNode root2 = new TreeNode(3);
        TreeNode n9 = new TreeNode(5);
        TreeNode n10 = new TreeNode(1);
        TreeNode n11 = new TreeNode(6);
        TreeNode n12 = new TreeNode(7);
        TreeNode n13 = new TreeNode(4);
        TreeNode n14 = new TreeNode(2);
        TreeNode n15 = new TreeNode(9);
        TreeNode n16 = new TreeNode(8);
        root2.left = n9;
        root2.right = n10;
        n9.left = n11;
        n9.right = n12;
        n10.left = n13;
        n10.right = n14;
        n14.left = n15;
        n14.right = n16;
        System.out.println(leafSimilar(root1, root2));
    }
}
